package com.xiangban.yanglaodajie_admin.service;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiangban.yanglaodajie_admin.dao.TeacherMapper;
import com.xiangban.yanglaodajie_admin.entity.Teacher;
import com.xiangban.yanglaodajie_admin.entity.TeacherShop;
import com.xiangban.yanglaodajie_admin.entity.dto.TeacherDto;
import com.xiangban.yanglaodajie_admin.entity.res.ApiResultCode;
import com.xiangban.yanglaodajie_admin.exception.BizException;
import com.xiangban.yanglaodajie_admin.utils.CommonUtils;
import com.xiangban.yanglaodajie_admin.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

import java.util.List;

import static com.xiangban.yanglaodajie_admin.utils.CommonUtils.BizEmpty;

/**
 * @Author Mr. MaYaYun
 * @Data 2021/7/8 17:40
 */
@Service
public class TeacherService {

    @Autowired
    private CommonUtils commonUtils;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private PageUtil pageUtil;
    @Autowired
    private TeacherMapper teacherMapper;


    /**
     * 参数：商家id、（课程id）
     * 教师列表
     **/
    public PageInfo<TeacherDto> TeacherList(TeacherDto teacher) {
        if (BizEmpty(commonUtils.getShopId(request))) {
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        PageHelper.startPage(pageUtil.getPage().getPageNum(), pageUtil.getPage().getPageSize());
        teacher.setShopId(Integer.parseInt(commonUtils.getShopId(request)));
        List<TeacherDto> teachers = teacherMapper.selectTeacherList(teacher);

        for (TeacherDto teacher1 : teachers) {
            Object parse = JSON.parse(teacher1.getExperience());
            teacher1.setExperiences((List<String>) parse);
            teacher1.setExperience(null);
            Object parse1 = JSON.parse(teacher1.getTag());
            teacher1.setTags((List<String>) parse1);
            teacher1.setTag(null);
//            //课程id不为空
//            if (teacher.getScheduleId() != null) {
//                TeacherShop teacherShop = new TeacherShop();
//                teacherShop.setShopId(teacher.getShopId());
//                teacherShop.setTeacherId(teacher1.getId());
//                teacherShop.setScheduleId(teacher.getScheduleId());
//                //查询课程绑定的教师
//                TeacherShop teacherShop1 = teacherMapper.selectBindTeacherShop(teacherShop);
//                if (teacherShop1 != null) {
//                    teacher1.setScheduleId(teacher.getScheduleId());
//                    teacher1.setRole(teacherShop1.getRole());
//                }
//            }
        }
        return new PageInfo<>(teachers);
    }

    /**
     * 增加教师信息
     **/
    @Transactional
    public void addTeacher(TeacherDto teacher) {

            if (BizEmpty(commonUtils.getShopId(request))) {
                throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
            }
            if (BizEmpty(teacher.getPhone())) {
                throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
            }
            if (BizEmpty(teacher.getName())) {
                throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
            }
            if (BizEmpty(teacher.getIntroduce())) {
                throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
            }
            if (BizEmpty(teacher.getTypeIntroduce())) {
                throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
            }
            if (BizEmpty(teacher.getJobTime())) {
                throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
            }
            if (BizEmpty(teacher.getTags())) {
                throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
            }
            if (BizEmpty(teacher.getExperiences())) {
                throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
            }
            teacher.setStates(0);

            String s = JSON.toJSONString(teacher.getExperiences());
            teacher.setExperience(s);

            String s1 = JSON.toJSONString(teacher.getTags());
            teacher.setTag(s1);


            teacher.setShopId(Integer.parseInt(commonUtils.getShopId(request)));
            Teacher teacher1 = teacherMapper.selectIsExit(teacher);
            if (teacher1 == null) {
                teacherMapper.saveTeacher(teacher);
            } else {
                throw new BizException(ApiResultCode.TEACHER_EXIST);
            }
    }

    /**
     * 修改教师信息
     **/
    public void updateTeacher(TeacherDto teacher) {
        if (BizEmpty(commonUtils.getShopId(request))) {
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(teacher.getId())) {
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(teacher.getStates())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (teacher.getExperiences() != null && teacher.getExperiences().size() > 0) {
            String s = JSON.toJSONString(teacher.getExperiences());
            teacher.setExperience(s);
        }
        if (teacher.getTags() != null && teacher.getTags().size() > 0) {
            String s = JSON.toJSONString(teacher.getTags());
            teacher.setTag(s);
        }
        teacher.setShopId(Integer.parseInt(commonUtils.getShopId(request)));
        teacherMapper.updateTeacher(teacher);
    }

    /**
     * 教师删除
     **/
    public void removeTeacher(TeacherDto teacher) {
        if (BizEmpty(commonUtils.getShopId(request))) {
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(teacher.getTeacherId())) {
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        teacher.setShopId(Integer.parseInt(commonUtils.getShopId(request)));
        teacherMapper.deleteTeacher(teacher);
    }

    /**
     * 教师信息详情
     **/
    public Object TeacherInfo(Teacher teacher) {
        if (BizEmpty(commonUtils.getShopId(request))) {
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(teacher.getId())) {
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        teacher.setShopId(Integer.parseInt(commonUtils.getShopId(request)));
        TeacherDto teacher1 = teacherMapper.selectTeacherInfo(teacher);
        teacher1.setExperiences((List<String>) JSON.parse(teacher1.getExperience()));
        teacher1.setTags((List<String>) JSON.parse(teacher1.getTag()));
        teacher1.setExperience(null);
        teacher1.setTag(null);
        return teacher1;
    }

    /**
     * 参数：课程id、教师id、角色、
     * 课程教师绑定
     **/
    @Transactional
    public void bindTeacher(TeacherShop teacher) {
        if (BizEmpty(commonUtils.getShopId(request))) {
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(teacher.getScheduleId())) {
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(teacher.getTeacherId())) {
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        teacher.setShopId(Integer.parseInt(commonUtils.getShopId(request)));

        if (teacher.getRole() != null && !teacher.getRole().toString().equals("") ){
            TeacherShop teacherShop = teacherMapper.selectBindTeacherShop(teacher);
            if (teacherShop != null) {
                //根据传来的绑定教师信息修改或删除  TODO
                teacherMapper.updateBindTeacherShop(teacher);
            } else {
                teacherMapper.insertTeacherShop(teacher);
            }
        }else {
            teacherMapper.deleteBindTeacher(teacher);
        }

    }

//    /**
//     * 课程教师或班长解除绑定
//     **/
//    public void overBindTeacher(TeacherShop teacher) {
//        if (BizEmpty(commonUtils.getShopId(request))) {
//            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
//        }
//        if (BizEmpty(teacher.getScheduleId())) {
//            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
//        }
//        teacher.setShopId(Integer.parseInt(commonUtils.getShopId(request)));
//        teacherMapper.deleteBindTeacher(teacher);
//    }
}
